package com.example.wangning.mycarofcountry.recommend.newest;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.example.mycarofcountry.entity.dao.Recommend_Newslist;
import com.example.wangning.mycarofcountry.R;
import com.example.wangning.mycarofcountry.cache.VolleySingleton;
import com.example.wangning.mycarofcountry.listener.OnMyClickListener;

import java.util.List;

/**
 * Created by dllo on 16/1/8.
 */
public class NewlistAdapter extends RecyclerView.Adapter<NewlistAdapter.NewlistViewHolder>  {
    private List<Recommend_Newslist> newslistBeans;
    private VolleySingleton queue;
    private ImageLoader imageLoader;
    private OnMyClickListener onMyClickListener;

    public void setOnMyClickListener(OnMyClickListener onMyClickListener) {
        this.onMyClickListener = onMyClickListener;
    }

    public NewlistAdapter(Context context) {
        //初始化请求队列
        queue = VolleySingleton.getVolleyIntance();
        //初始化图片缓存对象
        imageLoader = queue.getImageLoader();
    }

    public void addData(List<Recommend_Newslist> data) {
        newslistBeans = data;
        notifyDataSetChanged();//数据改变，通知页面刷新
    }

    @Override
    public NewlistViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());//获得布局加载器
        View view = inflater.inflate(R.layout.recycler_view_item, null);//加载行布局文件
        return new NewlistViewHolder(view);// 构建组件缓存对象并返回
    }

    @Override
    public void onBindViewHolder(NewlistViewHolder holder, int position) {
        if (newslistBeans != null && newslistBeans.size() > 0) {
            final Recommend_Newslist newslistBean = newslistBeans.get(position);

            holder.titleTv.setText(newslistBean.getTitle());
            holder.timeTv.setText(newslistBean.getTime());
            holder.replycountTv.setText(String.valueOf(newslistBean.getReplycount()) +"评论");
            if (newslistBean.getSmallpic() != null && newslistBean.getSmallpic().length() > 0) {
                String imgUrl = newslistBean.getSmallpic();
                // 网络拉取图片并显示
                queue.getBitmap(queue,imgUrl,holder.imageView);

            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onMyClickListener.onMyItemClick(newslistBean.getId());
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return newslistBeans != null && newslistBeans.size() > 0 ? newslistBeans.size() : 0;
    }


    class NewlistViewHolder extends RecyclerView.ViewHolder {
        TextView titleTv,timeTv,replycountTv;
        ImageView imageView;
        public NewlistViewHolder(View itemView) {
            super(itemView);
            titleTv = (TextView) itemView.findViewById(R.id.titleTv);
            timeTv = (TextView) itemView.findViewById(R.id.timeTv);
            replycountTv = (TextView) itemView.findViewById(R.id.replycountTv);
            imageView = (ImageView) itemView.findViewById(R.id.imgIv);
        }


    }


}
